package com.example.weatherapp.util;

import android.text.TextUtils;

import com.example.weatherapp.HourlyWeather;
import com.example.weatherapp.db.City;
import com.example.weatherapp.db.County;
import com.example.weatherapp.db.Province;
import com.example.weatherapp.gson.AQI;
import com.example.weatherapp.gson.Forecast;
import com.example.weatherapp.gson.NowWeather;
import com.example.weatherapp.gson.Suggest;
import com.example.weatherapp.gson.Tide;
import com.example.weatherapp.gson.Warning;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Utility {
    public static boolean headleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i=0;i<allProvinces.length();i++){
                    JSONObject provinceObject= allProvinces.getJSONObject(i);
                    Province province =new Province();
                    province.setProviceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.optInt("id"));
                    province.save();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    public static boolean headleCityResponse(String response,int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities = new JSONArray(response);
                for (int i=0;i<allCities.length();i++){
                    JSONObject cityeObject= allCities.getJSONObject(i);
                    City city =new City();
                    city.setProvinceId(provinceId);
                    city.setCityName(cityeObject.getString("name"));
                    city.setCityCode(cityeObject.optInt("id"));
                    city.save();
                }
                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return false;
    }
    public static boolean headleCountyResponse(String response,int cityId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i=0;i<allCounties.length();i++){
                    JSONObject countyObject= allCounties.getJSONObject(i);
                    County county=new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return false;
    }
    private  static Gson gson=new Gson();
    public static Forecast headleForecastResponse(String response){
        try {
            Forecast forecast=gson.fromJson(response,Forecast.class);
            return  forecast;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    public static AQI headleAQIResponse(String response){
        try {
            AQI aqi=gson.fromJson(response,AQI.class);
            return  aqi;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    public static Suggest headleSuggestResponse(String response){
        try {
            Suggest suggest=gson.fromJson(response,Suggest.class);
            return  suggest;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    public static NowWeather headleNowWeatherResponse(String response){
        try {
            NowWeather nowWeather=gson.fromJson(response,NowWeather.class);
            return  nowWeather;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    public static HourlyWeather headleHourlyWeatherResponse(String response) {
        try {
            return new Gson().fromJson(response, HourlyWeather.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Warning handleWarningResponse(String response) {
        try {
            return new Gson().fromJson(response, Warning.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Tide handleTideResponse(String response) {
        try {
            return new Gson().fromJson(response, Tide.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
